
#include "stdafx.h"
#include "FiniteStateMachine.h"
#include <stdio.h>
#include <windows.h>
#include <math.h>

HWND FindMyTopMostWindow()
{
	DWORD dwProcID = GetCurrentProcessId();
	HWND hWnd = GetTopWindow(GetDesktopWindow());
	while(hWnd)
	{
		DWORD dwWndProcID = 0;
		GetWindowThreadProcessId(hWnd, &dwWndProcID);
		if(dwWndProcID == dwProcID)
			return hWnd;            
		hWnd = GetNextWindow(hWnd, GW_HWNDNEXT);
	}
	return NULL;
}

bool keyPressed(int vKey) {
	if (GetForegroundWindow() == FindMyTopMostWindow())
		return (GetAsyncKeyState(vKey) == -32767);
	return false;
}

FiniteStateMachine::FiniteStateMachine(AudioWrapper* iAudioWrapper){
	using namespace State;
	mCurrentState = NOT_INITIALIZED;
	mAudioWrapper = iAudioWrapper;
	mExitStatus = 0;
}

void FiniteStateMachine::update() {

	char* filename = new char[255];
	bool actionTaken = true;

	using namespace State;
	switch (mCurrentState) {

	case NOT_INITIALIZED:
		if (!mAudioWrapper->initialize()) {
			std::cout << "Error initializing." << std::endl;
			mCurrentState = EXITING;
			mExitStatus = 1;
		} else {
			std::cout << "System initialized." << std::endl;
			mCurrentState = INITIALIZED;
		}
		break;

	case INITIALIZED:
		system("cls");
		std::cout << std::endl
				  << " Enter the name of a .wav to load it" << std::endl
				  << " you can use either an absolute path \"c:\\audio\\test.wav\"" << std::endl
				  << " or the relative path to the executable folder \"test.wav\"" << std::endl
				  << std::endl
				  << " >> ";

		FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
		std::cin.clear();
		std::cin.getline(filename, 255);

		if (mAudioWrapper->load(filename)) {
			mCurrentState = LOADED;
			std::cout << std::endl
					  << " File loaded." << std::endl;
			Sleep(1000);
		} else {
			std::cout << std::endl
				<< " File not found, or could not load it." << std::endl;
			Sleep(1000);
		}

		break;

	case LOADED:
		if (keyPressed(VK_ADD)) {
			// VOLUME +
			mAudioWrapper->setVolume( mAudioWrapper->getVolume() + 0.05f );
		} else if (keyPressed(VK_SUBTRACT)) {
			// VOLUME -
			mAudioWrapper->setVolume( mAudioWrapper->getVolume() - 0.05f );
		} else if (keyPressed(VK_NUMPAD6)) {
			// PITCH +
			mAudioWrapper->setPitch( mAudioWrapper->getPitch() + 0.05f );
		} else if (keyPressed(VK_NUMPAD4)) {
			// PITCH -
			mAudioWrapper->setPitch( mAudioWrapper->getPitch() - 0.05f );
		} else if (keyPressed(VK_RIGHT)) {
			// PAN RIGHT
			mAudioWrapper->setPan( mAudioWrapper->getPan() + 1.0f );
		} else if (keyPressed(VK_LEFT)) {
			// PAN LEFT
			mAudioWrapper->setPan( mAudioWrapper->getPan() - 1.0f );
		} else if (keyPressed('P')) {
			// PLAY/PAUSE
			mAudioWrapper->playPause();
		} else if (keyPressed('S')) {
			// STOP
			mAudioWrapper->stop();
		} else if (keyPressed('N')) {
			// LOAD NEW FILE
			mAudioWrapper->stop();
			mCurrentState = INITIALIZED;
		} else if (keyPressed('L')) {
			// TOGGLE LOOPING
			mAudioWrapper->toggleLooping();
		} else if (keyPressed(VK_ESCAPE)) {
			// EXIT
			std::cout << "Really exit? (ENTER = Bye Bye, ESC = Cancel)" << std::endl;
			mCurrentState = EXITING;
		} else {
			actionTaken = false;
		}

		break;

	case EXITING:
		if (keyPressed(VK_RETURN)) {
			// EXIT
			std::cout << " Bye!" << std::endl;
			Sleep(1000);
			mAudioWrapper->exit();
			exit(mExitStatus);
			
		} else if (keyPressed(VK_ESCAPE)) {
			// CANCEL
			mCurrentState = LOADED;
		}
	}

	if (mCurrentState==LOADED && actionTaken)
	{
		system("cls");

		std::cout << std::endl;
		std::cout << std::endl;

		std::cout << "================================================================================";
		std::cout << "                    OpenALWrapper test - Andrea Casaccia                        ";
		std::cout << "                  Sound Programming - MasterGD Verona 2012                      ";
		std::cout << "================================================================================";

		std::cout << std::endl;

		std::cout << "    P : (P)lay/pause                          Numpad +/- : change volume" << std::endl;
		std::cout << "    S : (S)top                                Numpad 4/6 : change pitch" << std::endl;
		std::cout << "    N : load (N)ew file                       Arrow Left/Right : change pan" << std::endl;
		std::cout << "    L : toggle (L)ooping                      Esc : (E)xit" << std::endl;

		std::cout << std::endl;

		std::cout << "================================================================================";

		std::cout << std::endl;

		std::cout << "    Looping = " << (mAudioWrapper->isLooping() ? "On" : "Off") << std::endl;
		std::cout << "    Volume = " << floorf(mAudioWrapper->getVolume() * 100 + 0.5) << "%" << std::endl;
		std::cout << "    Pitch = " << floorf(mAudioWrapper->getPitch() * 100 + 0.5) << "%" << std::endl;

		std::string pan = "Center";
		if ( mAudioWrapper->getPan() < 0 )
			pan = "Left";
		else if ( mAudioWrapper->getPan() > 0 )
			pan = "Right";
			

		std::cout << "    Pan = " << pan.c_str() << std::endl;

		std::cout << std::endl;

		std::cout << "================================================================================";

		std::cout << std::endl << "    ";

	}

	Sleep(30);
	delete filename;

}